<template>
    <my-input :id="id"  :options="options" :styles="styles"  :sign="sign" :time="time"  :valsql="valsql" :index="index" isChange="Y"  :data="data" :exkey="exkey" @input="changeData"></my-input>
</template>
<script>
import MyInput  from '../input/myInput'
export default {
    props:['id','styles','value','data','index','appid','valsql','sign','time','exkey'],
    name: "item-input",
    components: {
        MyInput
    },
     data(){
        return{
            options:[],
            depart:"",
            val:"",
            type:"",
            name:"",
            appids:"",
            sfItemData:[]
        }
    },
    created(){
        
        this.type=this.exkey.type;
        this.name=this.exkey.name;

        this.depart=this.data[this.exkey.pId][this.exkey.depart];
        this.appids=this.exkey.appid;
        this.loadItem(true);
    },
    methods:{
        changeData(data){
            console.log("selectdata=================="+data)
            let item=JSON.parse(data);
            this.val=item.val;
          
            if(this.sfItemData.length==0){
                this.loadItem(false);
            }else
                this.addItem(false);
            
        },
        loadItem(isFirst){
             this.$post(this.appids,"byg/funeral/getFuneralSfItem",{depart:this.depart}).then(data=>{
                this.sfItemData=[];
                for(let i=0;i<data.item.length;i++){
                    this.sfItemData.push(data.item[i]);
                }
               console.log("loadITem===============");
               console.log(this.sfItemData)
               if(!isFirst){
                   this.addItem(false)
               }
            });
        },
        addItem(isFirst){
           
            let isOk=true;
            let index=-1;
            for(let i=0;i<this.data["item"].length;i++){
                let szi_ex1=this.data["item"][i]["szi_ex1"];
                let szi_ex7=this.data["item"][i]["szi_ex7"];
                if((this.type&&this.type==szi_ex7)||(this.name&&this.name==szi_ex1)){
                    isOk=false;
                    this.$set(this.data["item"][i],"szi_ex2",this.val);
                    this.$set(this.data["item"][i],"szi_ex5",this.val);
                    index=i;
                    break;
                }
               
            }
            if(!this.val&&index!=-1){
                this.$delete(this.data["item"],index);
            }

            console.log("itemINput========")
            console.log(this.type+"=="+isOk+"=="+this.val)
            if(isOk&&this.val){
                
                let row=null;
                console.log(this.sfItemData)
                for(let i=0;i<this.sfItemData.length;i++){
                     let szi_ex1=this.sfItemData[i]["szi_ex1"];
                     let szi_ex7=this.sfItemData[i]["szi_ex7"];
                    if((this.type&&this.type==szi_ex7)||(this.name&&this.name==szi_ex1)){
                       row=JSON.parse(JSON.stringify(this.sfItemData[i]))
                    }
                }
                if(row){

                    row.szi_ex2=this.val;
                    row.szi_ex5=this.val;
                    this.$set(this.data["item"],this.data["item"].length,row)
                }
            }
        }
    }
}
</script>